package generator;

import java.io.File;
import java.util.Map;
import java.util.Properties;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;

import utils.PropertyLoader;

public class GenRunner {

	private static Model readModel(String umlModelFileAbsolutePath) {
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
				UMLPackage.eINSTANCE);
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
				.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);

		Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();

		// String path = "jar:file:/home/emilio/eclipse-distribuciones/Topcased-4.3.0-J2EE-GWT/plugins/org.eclipse.uml2.uml.resources_3.1.1.v201008191505.jar!/";
		String path = "jar:file:/media/Nuevo vol/eclipses/Topcased-4.3.0/plugins/org.eclipse.uml2.uml.resources_3.1.1.v201008191505.jar!/";

		URI uri = URI.createURI(path);
		uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri
				.appendSegment("libraries").appendSegment(""));
		uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri
				.appendSegment("metamodels").appendSegment(""));
		uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri
				.appendSegment("profiles").appendSegment(""));

		// ** File file = new File("models/TestingModel.uml");

		// URI testURI = URI.createURI("models/TestingModel.uml");
		// ** URI testURI = URI.createFileURI(file.getAbsolutePath());
		URI testURI = URI.createFileURI(umlModelFileAbsolutePath);

		Resource res = resourceSet.getResource(testURI, true);
		Model model = (Model) res.getContents().get(0);
		System.out.println(model.getName());
		System.exit(0);

		// URI basicProfileURI = URI.createURI("models/BasicProfile.uml");
		// Resource profileRes = resourceSet.getResource(basicProfileURI, true);
		// Profile basicProfile = (Profile)profileRes.getContents().get(0);
		// model.applyProfile(basicProfile);

		return model;
	}

	private static void generateModel(BaseTemplateClass templateClass, Model model) {
		EList<Element> elements = model.allOwnedElements();
		for (Element elt : elements) {
			templateClass.setElement(elt);
			if (templateClass.isProcessable()) {
				System.out.println(templateClass.returning());
				templateClass.createFile();
			}
		}
	}

	public static void main(String[] arg) {
		Properties prop = PropertyLoader.loadProperties("generator/mydat");
//		File file1 = new File("models/TestingModel.uml");
		File file2 = new File("models/ContactSJPA.uml");
//		Model umlModel1 = readModel(file1.getAbsolutePath());
		Model umlModel2 = readModel(file2.getAbsolutePath());
		
		JpaEntityTemplateClass jpaEntityTemplateClass = new JpaEntityTemplateClass(prop);
		generateModel(jpaEntityTemplateClass, umlModel2);
		// TemplateReader.searchTemplates(App.class, "services/");
	}
}